﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics;

using System.Runtime.InteropServices;

namespace Imagiality.MeshProcessing
{
    public struct point3d
    {
        public float x, y, z;
    }

    public unsafe struct pointcloud
    {
        public point3d* points;
        public int size;
    }

    public unsafe class PointCloud : NodeSceneObject, Renderable, Selectable
    {
        private Point3D[] points;
        private string pcdPath;

        private bool isSelected;
        private bool isMarked;

        [DllImport("DLL/PCLCommunicator.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern unsafe pointcloud* getPoints(String a);

        private pointcloud* data;

        public PointCloud(SceneObject parent, String pcdPath) : base(parent)
        {
            this.pcdPath = pcdPath;
        }

        private void readFromFile(String pcdPath)
        {
            data = getPoints(pcdPath);

            points = new Point3D[data->size];

            for (int i = 0; i < data->size; i++)
            {
                points[i] = new Point3D(this, data->points[i].x, data->points[i].y, data->points[i].z) ;
            }

        }

        public override IList<NodeSceneObject> GetChilds()
        {
            if (points == null)
                readFromFile(pcdPath);
           
            return points;
        }

        public void Display()
        {
            GetChilds();

            GL.PushMatrix();


            GL.Enable(EnableCap.AlphaTest);
            GL.Enable(EnableCap.PointSmooth);
            GL.PointSize(1);

            for (int i = 0; i < points.Length; i++)
            {
                points[i].Display();            
            }
            GL.PopMatrix();
        }


        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                isSelected = value;
            }
        }
        
        public bool IsMarked
        {
            get
            {
                return isMarked;
            }
            set
            {
                isMarked = value;
            }
        }

        public void Perform(SelectionOperation operation)
        {
            throw new NotImplementedException();
        }


      
    }
}
